# =============================================================================
# April 23 2025
# R code used to produce all results, tables, and figures in the SI Online Appendix B
# Rebecca Cordell
# Unpacking the Role of In-Group Bias in US Public Opinion on Human Rights Violations
# American Journal of Political Science
# https://doi.org/10.7910/DVN/TGAL7M
# =============================================================================

# Clear work environment
rm(list=ls())

options(scipen = 999)
options(warn=-1)

# -----------------------------------------------------------------------------
# Read in data
# -----------------------------------------------------------------------------

hr_survey<-read.csv("cordell_ingroupbiashumanrights_data.csv", header=TRUE, stringsAsFactors = FALSE)

# =============================================================================
# Table B.1: Summary of Respondents
# =============================================================================

# -----------------------------------------------------------------------------
# Calculate summary statistics for respondent variables
# -----------------------------------------------------------------------------

# Create some dummy variables
hr_survey$resp_female <- ifelse(hr_survey$resp_gender == "Female", 1, 0)
hr_survey$resp_male <- ifelse(hr_survey$resp_gender == "Male", 1, 0)
hr_survey$resp_first_gen <- ifelse(hr_survey$resp_citiz == "First generation", 1, 0)
hr_survey$resp_second_gen <- ifelse(hr_survey$resp_citiz == "Second generation", 1, 0)
hr_survey$resp_third_gen <- ifelse(hr_survey$resp_citiz == "Third generation", 1, 0)
hr_survey$resp_immig_citiz <- ifelse(hr_survey$resp_citiz == "Immigrant Citizen", 1, 0)
hr_survey$resp_immig_noncitiz <- ifelse(hr_survey$resp_citiz == "Immigrant non-citizen", 1, 0)
hr_survey$resp_christian <- ifelse(hr_survey$resp_relig == "Christian", 1, 0)
hr_survey$resp_jewish <- ifelse(hr_survey$resp_relig == "Jewish", 1, 0)
hr_survey$resp_muslim <- ifelse(hr_survey$resp_relig == "Muslim", 1, 0)
hr_survey$resp_buddhist <- ifelse(hr_survey$resp_relig == "Buddhist", 1, 0)
hr_survey$resp_hindu <- ifelse(hr_survey$resp_relig == "Hindu", 1, 0)
hr_survey$resp_atheist_agnost <- ifelse(hr_survey$resp_relig == "Atheist/Agnostic", 1, 0)
hr_survey$resp_no_hs <- ifelse(hr_survey$resp_educ == "No HS", 1, 0)
hr_survey$resp_hs_grad <- ifelse(hr_survey$resp_educ == "High school graduate", 1, 0)
hr_survey$resp_college <- ifelse(hr_survey$resp_educ == "College", 1, 0)
hr_survey$resp_postgrad <- ifelse(hr_survey$resp_educ == "Post-grad", 1, 0)
hr_survey$resp_income_less_20000 <- ifelse(hr_survey$resp_income == "$10,000 - $19,999", 1, 0)
hr_survey$resp_income_20000_49999 <- ifelse(hr_survey$resp_income == "$20,000-$49,999", 1, 0)
hr_survey$resp_income_50000_99999 <- ifelse(hr_survey$resp_income == "$50,000-$99,999", 1, 0)
hr_survey$resp_income_more_100000 <- ifelse(hr_survey$resp_income == "$100,000 or more", 1, 0)
hr_survey$resp_very_liberal <- ifelse(hr_survey$resp_ideo == "Very liberal", 1, 0)
hr_survey$resp_liberal <- ifelse(hr_survey$resp_ideo == "Liberal", 1, 0)
hr_survey$resp_moderate <- ifelse(hr_survey$resp_ideo == "Moderate", 1, 0)
hr_survey$resp_conserv <- ifelse(hr_survey$resp_ideo == "Conservative", 1, 0)
hr_survey$resp_very_conserv <- ifelse(hr_survey$resp_ideo == "Very conservative", 1, 0)
hr_survey$resp_indep <- ifelse(hr_survey$resp_party3 == "Independent", 1, 0)

# -----------------------------------------------------------------------------
# Calculate summary statistics for respondent variables
# -----------------------------------------------------------------------------

# Create function to calculate mean, min, max, sd
get_summary_stats <- function(varname, label = varname) {
  vec <- hr_survey[[varname]]
  stats <- c(
    Mean = mean(vec, na.rm = TRUE),
    Min = min(vec, na.rm = TRUE),
    Max = max(vec, na.rm = TRUE),
    SD = sd(vec, na.rm = TRUE)
  )
  out <- as.data.frame(t(stats))
  rownames(out) <- label
  return(out)
}

# Create list of variables
vars <- c(
  "resp_age" = "Age",
  "resp_female" = "Female",
  "resp_male" = "Male",
  "resp_white" = "White",
  "resp_black" = "Black",
  "resp_hispanic" = "Hispanic",
  "resp_asian" = "Asian",
  "resp_native" = "Native American",
  "resp_middle_east" = "Middle Eastern",
  "resp_christian" = "Christian",
  "resp_jewish" = "Jewish",
  "resp_muslim" = "Muslim",
  "resp_buddhist" = "Buddhist",
  "resp_hindu" = "Hindu",
  "resp_atheist_agnost" = "Atheist/Agnostic",
  "resp_first_gen" = "First generation immigrant",
  "resp_second_gen" = "Second generation immigrant",
  "resp_third_gen" = "Third generation immigrant",
  "resp_immig_citiz" = "Immigrant Citizen",
  "resp_immig_noncitiz" = "Immigrant Non-citizen",
  "resp_no_hs" = "No high school",
  "resp_hs_grad" = "High school graduate",
  "resp_college" = "College",
  "resp_postgrad" = "Postgraduate",
  "resp_income_less_20000" = "Family income <$20,000",
  "resp_income_20000_49999" = "Family income $20,000-$49,999",
  "resp_income_50000_99999" = "Family income $50,000-$99,999",
  "resp_income_more_100000" = "Family income >$100,000",
  "resp_very_liberal" = "Very liberal",
  "resp_liberal" = "Liberal",
  "resp_moderate" = "Moderate",
  "resp_conserv" = "Conservative",
  "resp_very_conserv" = "Very conservative",
  "resp_dem" = "Democrat",
  "resp_indep" = "Independent",
  "resp_rep" = "Republican"
)

# Apply function and combine data
tabb1 <- do.call(rbind, Map(get_summary_stats, names(vars), vars))
tabb1 <- round(tabb1, 3)
rownames(tabb1) <- unname(vars)

# -----------------------------------------------------------------------------
# Create table
# -----------------------------------------------------------------------------

# Print Table B.1
write.csv(tabb1, "tab_b1.csv", row.names=TRUE)